package com.boge.study.design_pattern.proxy.jdk_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class JdkProxyTest {
	
	public static void main(String[] args) {
		CarService car = new CarServiceImpl();

		//事务处理器（需要实现的代理功能）
		InvocationHandler handler = new TimeHandler(car);
		
		Class clazz = car.getClass();

		//生成动态代理类
		//Proxy为动态代理类，newProxyInstance()返回代理类的一个实例
		CarService timeProxy = (CarService) Proxy.newProxyInstance(
				clazz.getClassLoader(), 
				clazz.getInterfaces(), 
				handler);
//		timeProxy.move();
		
		//日志代理先执行
		InvocationHandler handler2 = new LogHandler(timeProxy);

		CarService logProxy = (CarService) Proxy.newProxyInstance(
				clazz.getClassLoader(),
				clazz.getInterfaces(),
				handler2);
		logProxy.move();
		
	}

}
